function GetEntitiesForTeleport(controller)
	local constraption = constraint.GetAllConstrainedEntities(controller) 
	return constraption
end

function Jump(ply)
	if (ply.lastjump < (CurTime() + 15)) then
		local posply
		for _,controler in pairs(servertable["ship"]["controller"]) do
			if (IsOwner(controler,ply)) then
				posply = controler:GetPos()
			end
		end
		if (posply == nil) then
			ply:SendLua("GAMEMODE:AddNotify(\"" ..  "You need a Test Ent to use Hyperspace" .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
			return
		end
		for index,Gate in pairs(servertable["map"]["jumpgates"]) do
			local posgate = Gate:GetPos()
			local distance = posply:Distance( posgate )
			if (distance < servertable["map"]["maxjumpdistance"]) then
				local name = Gate:GetName( )
				local splitname = string.Explode(" ",tostring(name))
				for index2,Gate2 in pairs(servertable["map"]["jumpgates"]) do
					if not (Gate2 == Gate) then
						local name2 = Gate2:GetName( )
						local splitname2 = string.Explode(" ",tostring(name2))
						if (splitname[2] == splitname2[2]) then
							if (splitname[3] == "true") then
								local jumpvector = (Gate2:GetPos() - Gate:GetPos())
								local posgate2 = Gate2:GetPos()
								Msg("Vector " .. tostring(jumpvector) .. "\n")
								DoJump(jumpvector,ply)
								ply.lastjump = CurTime()
								timer.Create("Jumptimer" .. ply:GetName(),3,1,GetSector,ply)
								return
							else
								ply:SendLua("GAMEMODE:AddNotify(\"" ..  "This Gate can not be activated" .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
							end
						end
					end
				end
				ply:SendLua("GAMEMODE:AddNotify(\"" ..  "Gate Failture, Destination not found" .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
			end
		end
		ply:PrintMessage(HUD_PRINTCENTER, "No Gate nearby" )
	else
		ply:PrintMessage(HUD_PRINTCENTER, "Hyperdrive is still cooling down" )
	end
end
concommand.Add("Jump",Jump) 

function DoJump(jumpvector,ply)
	for _,controler in pairs(servertable["ship"]["controller"]) do
		if IsOwner(controler,ply) then
			local toteleport = GetEntitiesForTeleport(controler)
			local startpos = controler:GetPos()
			for index,ent in pairs(toteleport) do
				local physobj= ent:GetPhysicsObject( )
				physobj:EnableMotion(  false )
			end
			for index,ent in pairs(toteleport) do
				local pos4 = ent:GetPos()									
				local effectdata = EffectData()
				effectdata:SetStart(pos4)
				effectdata:SetOrigin(pos4)
				effectdata:SetScale( 200 )
				effectdata:SetMagnitude( 40 )
				effectdata:SetEntity(ent)
				util.Effect( "transportcore", effectdata )
				if (ent.Pos) then
					ent.Pos = ent.Pos + jumpvector
				end
				if (ent.Pos) then
					ent.power = 0
				end
				local start = ent:GetPos() 
				ent:SetPos(start + jumpvector) 
			end
			for index,ent in pairs(toteleport) do
				local pos4 = ent:GetPos()									
				local effectdata = EffectData()
				effectdata:SetStart(pos4)
				effectdata:SetOrigin(pos4)
				effectdata:SetScale( 200 )
				effectdata:SetMagnitude( 40 )
				effectdata:SetEntity(ent)
				util.Effect( "transportcore", effectdata )
				local physobj= ent:GetPhysicsObject( )
				physobj:EnableMotion(  true )
			end
			local startpos = controler:GetPos()
		end
	end
end

function RegisterGates( )
	local cacheedents = ents.GetAll()
	for index,entity in pairs(cacheedents) do
		local name = entity:GetName( )
		local splitname = string.Explode(" ",tostring(name))
		if (splitname[1] == "Jumpgate") then
			AddLight(entity:GetPos(),"0 255 0")
			table.insert(servertable["map"]["jumpgates"],entity)
		end
	end
	Msg("Registerd " .. table.getn(servertable["map"]["jumpgates"]) .. " Jumpgates\n")
end
hook.Add( "InitPostEntity", "RegisterGates", RegisterGates );  